package com.wyz.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.jasper.tagplugins.jstl.core.Out;

import com.wyz.bean.Record;
import com.wyz.entity.Movie;
import com.wyz.entity.Review;
import com.wyz.entity.User;
import com.wyz.json.Jackson;
import com.wyz.service.MovieService;
import com.wyz.service.ReviewService;
import com.wyz.service.UserService;

import my.mvc.IUser;
import my.mvc.RequestContext;
import my.util.ResponseUtils;
import sun.java2d.pipe.OutlineTextRenderer;

public class ReviewAction {
	
	private ReviewService reviewService =new ReviewService();
	private UserService userService=new UserService();
	private MovieService movieService = new MovieService();
	public void save(RequestContext rc) throws ServletException, IOException{
		
		Review review=rc.form(Review.class);
		reviewService.save(review);
		
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
		
	}
	
	public void findAll(RequestContext rc){
		
		List<Review> list = reviewService.findAll();
		
		String json = Jackson.me().toJson(list);
		ResponseUtils.renderJson(rc.response(), json);
	}
	public void findById(RequestContext rc)throws ServletException, IOException{
		Long id=rc.param("id",0L);
		Review review=reviewService.findById(id);
		IUser iuser=rc.getUserFromCookie();
		if(iuser!=null) {
			User user=userService.findById(iuser.getId());
			rc.reqAttr("user",user);
		}
		rc.reqAttr("review",review);
		rc.forward("/pages/review.jsp");
	}
	
	public void findByMovieId(RequestContext rc)throws ServletException, IOException{
		Long movieId=rc.param("movieId",0L);
		List<Review> reviews=reviewService.findByMovieId(movieId);
		String json = Jackson.me().toJson(reviews);
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	public void addThumb_upAmount(RequestContext rc)throws ServletException ,IOException{
		Long id=rc.param("id",0L);
		Review review=reviewService.findById(id);
		reviewService.addThumb_upAmount(id, review.getThumb_upAmount()+1);
		String json = Jackson.me().toJson(new Record(true,review.getThumb_upAmount()+1,""));
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	public void reviewForm(RequestContext rc)  throws ServletException, IOException{
		
		Long movieId=rc.param("movieId",0L);
		Movie movie=movieService.findById(movieId);	
		IUser iuser=rc.getUserFromCookie();
		if(iuser!=null) {
			User user=userService.findById(iuser.getId());
			rc.reqAttr("user",user);
		}
		rc.reqAttr("movie",movie);
		rc.forward("/pages/review_form.jsp");
		
	}
	  
    public void findByUserId(RequestContext rc) throws ServletException, IOException{
    	Long userId=rc.param("userId", 0L);
    	User user=userService.findById(userId);
    	List<Review> reviews=reviewService.findByUserId(user.getId());
    	System.out.println("他有"+reviews.size());
    	String json = Jackson.me().toJson(reviews);
		ResponseUtils.renderJson(rc.response(), json);
    }
	
	
	
}
